import { createRouter, createWebHistory } from 'vue-router'
import My from '@/views/home/my.vue'
import Cart from '@/views/home/cart.vue'
import List from '@/views/home/list.vue'
import Cla from '@/views/home/cla.vue'
import Index from '@/views/home/index.vue'
import Home from '@/views/home/home.vue'
import login from '@/views/login/index.vue'
import xq from '@/views/xq/index.vue'
import lb from '@/views/lb/index.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/home',
      name: 'home',
      component: Home,children:[
            {
      path: 'index',
      name: 'index',
      component:Index,
    },
        {
      path: 'cla',
      name: 'cla',
      component: Cla,
    },
        {
      path: 'list',
      name: 'list',
      component: List,
    },
        {
      path: 'cart',
      name: 'cart',
      component: Cart,
    },
        {
      path: 'my',
      name: 'my',
      component: My,
    },
      ]
    },
        {
      path: '/login',
      name: 'login',
      component: login,
    },
            {
      path: '/xq/:goods_id',
      name: 'xq',
      component: xq,
    },
                {
      path: '/lb/:cat_name',
      name: 'lb',
      component: lb,
    },
  ],
})
let isAuthenticated=localStorage.getItem("token")
router.beforeEach((to, from, next) => {
  if ((to.name == 'cart'||to.name == 'my') && !isAuthenticated) next({ name: 'login' })
  else next()
})
export default router
